///// delete survey/////
function ajaxGetAnswer(element) {
    element.parent().parent().find(".namecol-mutil").removeClass("active");
    element.addClass("active");
    var data = element.parent().find(".dataanswer").html();

    zm.ajax({
        method: "POST",
        url: "getanswer",
        dataType: 'json',
        data: {data: data},
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            element.parent().parent().parent().find(".content-mutil-line-text").text(data.msg);
            element.parent().parent().parent().find(".content-mutil-line-text").removeClass("showboder");
            element.parent().parent().parent().find(".content-mutil-line-text").addClass("showboder");
            var leftelemt = element.parent().parent();
            var rightelemt = element.parent().parent().parent().find(".content-mutil-line-text");
            balance(leftelemt, rightelemt);

        }
    });
}
//////////////////////
function ajaxGetResponse(username, data, idUser,left,top) {
    zm.ajax({
        method: "POST",
        url: "getresponse",
        dataType: 'json',
        data: {data: data},
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            show(username, JSON.parse(data.msg), idUser, left, top);
           
        }
    });
}

//////////////////home sort view/////////////////////
function ajaxSortNewHome(e, by) {
    var container = zm(".newest_survey").find(".tableContainer");
    if (zm(".newest_survey").find(".pageNumbers").size() === 0)
        page = 1;
    else
        var page = zm(".pageNumbers").find("a.active").text();
    var tmp = e.attr("asc");
    var asc = (tmp === "true") ? false : true;
    zm.ajax({
        method: "POST",
        url: "paging?&page=" + page + "&type=NEW&by=" + by + "&asc=" + asc,
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            zm(".view-header-new").unbindAll();
            zm(".res-header-new").unbindAll();

            container.html(data);
            if (by === "VIEW") {
                container.find(".view-header-new").attr("asc", asc);

            }
            else {
                container.find(".res-header-new").attr("asc", asc);
            }
            zm(".view-header-new").bind("click", function() {
                zm(".view-header-new").unbindAll();
                ajaxSortNewHome(zm(this), "VIEW");
            });

            zm(".res-header-new").bind("click", function() {
                zm(".res-header-new").unbindAll();
                ajaxSortNewHome(zm(this), "RES");
            });

        }
    });
}


function ajaxSortHotHome(e, by) {
    var container = zm(".hotest_survey").find(".tableContainer");
    if (zm(".hotest_survey").find(".pageNumbers").size() === 0)
        page = 1;
    else
        var page = zm(".pageNumbers").find("a.active").text();
    var tmp = e.attr("asc");
    var asc = (tmp === "true") ? false : true;
    zm.ajax({
        method: "POST",
        url: "paging?&page=" + page + "&type=HOT&by=" + by + "&asc=" + asc,
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            zm(".view-header-hot").unbindAll();
            zm(".res-header-hot").unbindAll();
            container.html(data);
            if (by === "VIEW") {
                container.find(".view-header-hot").attr("asc", asc);



            }
            else {
                container.find(".res-header-hot").attr("asc", asc);

            }
            zm(".view-header-hot").bind("click", function() {
                zm(".view-header-hot").unbindAll();
                ajaxSortHotHome(zm(this), "VIEW");
            });

            zm(".res-header-hot").bind("click", function() {
                zm(".res-header-hot").unbindAll();
                ajaxSortHotHome(zm(this), "RES");
            });
        }
    });
}
/////////////////////////////////////////////////////



///// delete survey/////
function ajaxDelSurvey() {
    var idSurvey = zm("#survey").attr("idsurvey");
    zm.ajax({
        method: "POST",
        url: "delsur",
        data: {idSurvey: idSurvey},
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            zm.Boxy.alert("Khảo sát của bạn đã được xóa!", "Thành công");
        }
    });
}
//////////////////////





///////////delete response////////

function ajaxDelResponse(idSurvey, arrID) {
    zm.ajax({
        method: "POST",
        url: "delres",
        dataType: 'json',
        data: {idSurvey: idSurvey, arrID: arrID},
        success: function(data) {
            if (data.idUser.length > 0) {

                for (var i = 0; i < data.idUser.length; i++) {
                    zm(".row-table").each(function() {
                        if (parseInt(zm(this).attr("iduser")) === data.idUser[i])
                            zm(this).remove();
                    });
                }
                zm.Boxy.alert("Đã xóa thành công!", "Thông báo");
            }
            else {
                zm.Boxy.alert("Xóa thất bại!", "Lỗi");
            }
        }
    });
}

///////////////////////////////////

//////////////////////home hostes paging////////////////

function  ajaxHostestHome(pageContainer, page) {

    zm.ajax({
        method: "POST",
        url: "paging?" + "&page=" + page + "&type=HOT",
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            pageContainer.parent().find(".tableContainer").html(data);
            raiseSortEvent();
        }
    });
}
///////////////////////////////

///////////////////// response detail paing/////////////

function ajaxResDetail(pageContainer, page) {
    var tableparent = pageContainer.parent().find('.responseContainer');
    var idSurvey = zm('#survey').attr('idsurvey');
    zm.ajax({
        method: "POST",
        url: "responsedetail?idSurvey=" + idSurvey + "&page=" + page,
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            tableparent.html(data);
            zm(".namecol-res").unbindAll();
            zm(".namecol-res").bind("click", function() {

                raiseClickResponseEvent(zm(this));
            });
        }
    });


}

/////////////////////////////////////////////////////////


//////////////////home newset paging/////////////////////
function ajaxNewestHome(pageContainer, page) {

    zm.ajax({
        method: "POST",
        url: "paging?" + "&page=" + page + "&type=NEW",
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            pageContainer.parent().find(".tableContainer").html(data);
            raiseSortEvent();
        }
    });
}
/////////////////////////////////////////////////////

//////////////////////ajax statistic////////////////////////////////
function ajaxStat(pageContainer, page) {
    var url = "";
    var questionContainer = pageContainer.parent();
    var idSurvey = zm('#survey').attr('idsurvey');
    var idQuestion = questionContainer.attr('idquestion');
    url = "answer?idSurvey=" + idSurvey + "&idQuestion=" + idQuestion + "&page=" + page;
    if (pageContainer.parent().find(".table-mutil-line").size() === 1)
        url += "&type=" + multiline;
    zm.ajax({
        method: "POST",
        url: url,
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            questionContainer.find(".answer-stats").html(data);
            zm(".namecol-mutil").unbindAll();

            zm(".namecol-mutil").bind("click", function() {
                ajaxGetAnswer(zm(this));
            });
        }
    });
}
///////////////////////////////////////////////////////////////


//////////////add new survey//////////
function ajaxAddSurvey(JSONSurvey) {
    zm.ajax({
        method: "POST",
        url: "add",
        dataType: 'json',
        data: {survey: JSONSurvey},
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            zm.Boxy.alert(data.msg, "Thông báo");
        }
    });
}

///////////////////////////////////

////////////////edit survey//////
function ajaxEditSurvey(idSurvey, JSONSurvey) {
    zm.ajax({
        method: "POST",
        url: "edited",
        dataType: 'json',
        data: {idSurvey: idSurvey, survey: JSONSurvey},
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert("Vui lòng thử lại sau!<br/>", "Lỗi");
                return;
            }
            zm.Boxy.alert(data.msg, "Thành công");
        }
    });
}

///////////////////////////////

///////////////submit response///////////
function ajaxSubmitResponse(idSurvey, JSONResponse) {
    zm.ajax({
        method: "POST",
        url: "submit",
        dataType: 'json',
        data: {idSurvey: idSurvey, response: JSONResponse},
        success: function(data) {
            if (data.error === "ERROR") {
                zm.Boxy.alert(data.msg, "Lỗi");
                return;
            }
            zm.Boxy.alert(data.msg, "Thành công");

        }
    });
}
///////////////////////////////////////